﻿
using Assets.IOLibrary.Global;
using Assets.IOLibrary.Model;
using DataSourceLibray;
using DataSourceLibray.Handler;
using DataSourceLibray.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Assets.IOLibrary
{
    public class AppSettings : IDisposable
    {
        #region Default

        private static AppSettings instance;
        private static readonly object obj = new object();
        private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        public static AppSettings Instance
        {
            get
            {
                if (instance == null)
                    lock (obj)
                    {
                        if (instance == null)
                            instance = new AppSettings();
                    }
                return instance;
            }
        }

        public void Dispose()
        {
            this.cancellationTokenSource.Cancel();
        }

        #endregion

        public Alarm Alarm { get; set; } = new Alarm();

        public Model.IO Io { get; private set; } = Model.IO.Instance;

        public SYS Sys { get; private set; } = SYS.Instance;

        private AppSettings()
        {
            var param = new S7PlcParam[] {
                 new S7PlcParam {
                    CpuType=S7.Net.CpuType.S71200,
                    Ip="192.168.1.230",
                    Rack=0,
                    Slot=0,
                    CustomName="230",
                },
                  new S7PlcParam {
                    CpuType=S7.Net.CpuType.S71200,
                    Ip="192.168.1.216",
                    Rack=0,
                    Slot=0,
                    CustomName="216",
                },
            };
            S7PlcManager.Instance.Init(param);
        }

        public void Init()
        {
            UpdatePlc();
        }

        private void UpdatePlc()
        {
            Task.Factory.StartNew(async () =>
            {
                var mp101 = new Mapping101();
                var mp102 = new Mapping102();
                var mp103 = new Mapping103();
                var mp201 = new Mapping201();
                var mp202 = new Mapping202();//内部变量
                while (!cancellationTokenSource.IsCancellationRequested)
                {
                    bool isOk = false;
                    try
                    {
                        var plc = S7PlcManager.Instance.GetPlc("216");
                        isOk = plc.IsConnected;

                        if (isOk)
                        {
                            var arry = mp101.GetDeviceDescription().ToArray();
                            var list = plc.ReadListByAddress(true, arry);
                            isOk = list != null;
                            if (isOk)
                            {
                                mp101.SetDevicePropertieByDescription(list);
                                Io.PLC.PLC101.MapperFrom(mp101);

                            }
                            arry = mp102.GetDeviceDescription().ToArray();
                            list = plc.ReadListByAddress(true, arry);
                            isOk = list != null;
                            if (isOk)
                            {
                                mp102.SetDevicePropertieByDescription(list);
                                Io.PLC.PLC102.MapperFrom(mp102);
                            }

                            arry = mp103.GetDeviceDescription().ToArray();
                            list = plc.ReadListByAddress(true, arry);
                            isOk = list != null;
                            if (isOk)
                            {
                                mp103.SetDevicePropertieByDescription(list);
                                Io.PLC.PLC103.MapperFrom(mp103);
                            }

                            arry = mp201.GetDeviceDescription().ToArray();
                            list = plc.ReadListByAddress(true, arry);
                            isOk = list != null;
                            if (isOk)
                            {
                                mp201.SetDevicePropertieByDescription(list);
                                Io.PLC.PLC201.MapperFrom(mp201);

                            }
                            arry = mp202.GetDeviceDescription().ToArray();
                            list = plc.ReadListByAddress(true, arry);
                            isOk = list != null;
                            if (isOk)
                            {
                                mp202.SetDevicePropertieByDescription(list);
                                Io.PLC.PLC202.MapperFrom(mp202);

                            }

                        }
                    }
                    catch (Exception)
                    {

                    }
                    if (isOk)
                    {
                        #region 101

                        if (Sys.IO.PLC101.Flag > 0)
                        {
                            Sys.IO.PLC101.Flag = 2;
                        }
                        else
                        {
                            Sys.IO.PLC101.Flag = 1;
                        }
                        #endregion

                        #region 102
                        if (Sys.IO.PLC102.Flag > 0)
                        {
                            Sys.IO.PLC102.Flag = 2;
                        }
                        else
                        {
                            Sys.IO.PLC102.Flag = 1;
                        }
                        #endregion

                        #region 103
                        if (Sys.IO.PLC103.Flag > 0)
                        {
                            Sys.IO.PLC103.Flag = 2;
                        }
                        else
                        {
                            Sys.IO.PLC103.Flag = 1;
                        }
                        #endregion

                        #region 201

                        if (Sys.IO.PLC201.Flag > 0)
                        {
                            Sys.IO.PLC201.Flag = 2;
                        }
                        else
                        {
                            Sys.IO.PLC201.Flag = 1;
                        }
                        #endregion

                        #region 202
                        if (Sys.IO.PLC202.Flag > 0)
                        {
                            Sys.IO.PLC202.Flag = 2;
                        }
                        else
                        {
                            Sys.IO.PLC202.Flag = 1;
                        }
                        #endregion
                    }
                    else
                    {
                        #region 101


                        if (Sys.IO.PLC101.Flag > 0)
                        {
                            Sys.IO.PLC101.Flag = 0;
                        }
                        else if (Sys.IO.PLC101.Flag > -9999)
                        {
                            Sys.IO.PLC101.Flag--;
                        }
                        #endregion

                        #region 102

                        if (Sys.IO.PLC102.Flag > 0)
                        {
                            Sys.IO.PLC102.Flag = 0;
                        }
                        else if (Sys.IO.PLC102.Flag > -9999)
                        {
                            Sys.IO.PLC102.Flag--;
                        }

                        #endregion

                        #region 103
                        if (Sys.IO.PLC103.Flag > 0)
                        {
                            Sys.IO.PLC103.Flag = 0;
                        }
                        else if (Sys.IO.PLC103.Flag > -9999)
                        {
                            Sys.IO.PLC103.Flag--;
                        }
                        #endregion

                        #region 201
                        if (Sys.IO.PLC201.Flag > 0)
                        {
                            Sys.IO.PLC201.Flag = 0;
                        }
                        else if (Sys.IO.PLC201.Flag > -9999)
                        {
                            Sys.IO.PLC201.Flag--;
                        }
                        #endregion

                        #region 202
                        if (Sys.IO.PLC202.Flag > 0)
                        {
                            Sys.IO.PLC202.Flag = 0;
                        }
                        else if (Sys.IO.PLC202.Flag > -9999)
                        {
                            Sys.IO.PLC202.Flag--;
                        }
                        #endregion
                    }
                    await Task.Delay(5000);
                }


            }, cancellationTokenSource.Token);
        }



    }
}
